﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Collections;
using System.Configuration;
using System.Diagnostics;
using System.Threading;


namespace OneKeyL
{

    public partial class Form1 : Form
    {
        public static string username;
        public static string password;
        public static string signflag;
        bool hasStart = false;
        bool hasInput = false;
        bool hasGolist = false;
        bool hasGolist2 = false;

        List<MyUrl> myUrlList = new List<MyUrl>();
        public Form1()
        {
            InitializeComponent();
            myUrlList.Add(new MyUrl() { Title = "整站", Url = "http://www.baidu.com/s?wd=site:autohome.com.cn" });
            myUrlList.Add(new MyUrl() { Title = "资讯", Url = "http://www.baidu.com/s?wd=site:www.autohome.com.cn" });
            myUrlList.Add(new MyUrl() { Title = "视频", Url = "http://www.baidu.com/s?wd=site:v.autohome.com.cn" });
            myUrlList.Add(new MyUrl() { Title = "二手车", Url = "http://www.baidu.com/s?wd=site:2sc.autohome.com.cn" });
            myUrlList.Add(new MyUrl() { Title = "搜索", Url = "http://www.baidu.com/s?wd=site:sou.autohome.com.cn" });
            myUrlList.Add(new MyUrl() { Title = "产品库", Url = "http://www.baidu.com/s?wd=site:car.autohome.com.cn" });
            myUrlList.Add(new MyUrl() { Title = "用品库", Url = "http://www.baidu.com/s?wd=site:Y.autohome.com.cn" });
            myUrlList.Add(new MyUrl() { Title = "论坛", Url = "http://www.baidu.com/s?wd=site:club.autohome.com.cn" });
            myUrlList.Add(new MyUrl() { Title = "个人中心", Url = "http://www.baidu.com/s?wd=site:i.autohome.com.cn" });
            myUrlList.Add(new MyUrl() { Title = "团购", Url = "http://www.baidu.com/s?wd=site:tuan.autohome.com.cn" });
            myUrlList.Add(new MyUrl() { Title = "经销商", Url = "http://www.baidu.com/s?wd=site:dealer.autohome.com.cn" });
            myUrlList.Add(new MyUrl() { Title = "租车", Url = "http://www.baidu.com/s?wd=site:zuche.autohome.com.cn" });
            myUrlList.Add(new MyUrl() { Title = "整站谷歌", Url = "http://www.google.com.hk/#hl=zh-CN&source=hp&q=site:autohome.com.cn" });
            myUrlList.Add(new MyUrl() { Title = "整站谷歌", Url = "http://www.google.com.hk/#hl=zh-CN&source=hp&q=site:www.autohome.com.cn" });
            myUrlList.Add(new MyUrl() { Title = "资讯", Url = "http://www.google.com.hk/#hl=zh-CN&source=hp&q=site:v.autohome.com.cn" });
            myUrlList.Add(new MyUrl() { Title = "视频", Url = "http://www.google.com.hk/#hl=zh-CN&source=hp&q=site:2sc.autohome.com.cn" });
            myUrlList.Add(new MyUrl() { Title = "二手车", Url = "http://www.google.com.hk/#hl=zh-CN&source=hp&q=site:sou.autohome.com.cn" });
            myUrlList.Add(new MyUrl() { Title = "搜索", Url = "http://www.google.com.hk/#hl=zh-CN&source=hp&q=site:car.autohome.com.cn" });
            myUrlList.Add(new MyUrl() { Title = "产品库", Url = "http://www.google.com.hk/#hl=zh-CN&source=hp&q=site:Y.autohome.com.cn" });
            myUrlList.Add(new MyUrl() { Title = "用品库", Url = "http://www.google.com.hk/#hl=zh-CN&source=hp&q=site:club.autohome.com.cn" });
            myUrlList.Add(new MyUrl() { Title = "论坛", Url = "http://www.google.com.hk/#hl=zh-CN&source=hp&q=site:i.autohome.com.cn" });
            myUrlList.Add(new MyUrl() { Title = "个人中心", Url = "http://www.google.com.hk/#hl=zh-CN&source=hp&q=site:tuan.autohome.com.cn" });
            myUrlList.Add(new MyUrl() { Title = "团购", Url = "http://www.google.com.hk/#hl=zh-CN&source=hp&q=site:dealer.autohome.com.cn" });
            myUrlList.Add(new MyUrl() { Title = "经销商", Url = "http://www.google.com.hk/#hl=zh-CN&source=hp&q=site:zuche.autohome.com.cn" });
            myUrlList.Add(new MyUrl() { Title = "租车", Url = "http://www.google.com.hk/#hl=zh-CN&source=hp&q=site:zuche.autohome.com.cn" });



            webBrowser1.Url = new Uri("http://edit.autohome.com.cn");
            webBrowser1.ScriptErrorsSuppressed = true;

        }

        void timer2_Tick(object sender, EventArgs e)
        {
            SignOut();

        }

        void timer_Tick(object sender, EventArgs e)
        {
            SignIn();


        }

        private void Form1_Load(object sender, EventArgs e)
        {
            webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
        }


        private void button1_Click(object sender, EventArgs e)
        {
            SignIn();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SignOut();
        }
        public string CurrentData = "2011-10-22";
        public string GetVisitSourceData()
        {
            string postData = "Content-Type: application/x-www-form-urlencoded Content-Length: 650 __VIEWSTATE=JudEeGYjMIBO47PYD24K5I%2FvLgEOqTbMHT%2BkWfW0OTEW%2FHB0B4%2B64sUrHoNl7i%2BnOJc%2FCIXxihrbzz9FhNpclc8dRDL%2BZ4u%2FbGtaK%2FylPWLf4a4sZhX%2FOnoUxlIQYc%2B5tIuRBJ81tzpEufRPRz4thUDRtLxHp385jqBpINVJyWEYVqeirZNHFI3uoyW%2Fvt1Lzbgnc6O0bHhasgE3IbvKkYPCF18bAkj%2FFNHyaw%3D%3D&__EVENTVALIDATION=CdqGRY9mxFNpeSwLdH6yUTkB78t9iNuLzqlpi%2FG1uzlifzhxvtAFilxb75kuLU6q%2FIQ2g5ovl%2F%2FOFq00Gv2GZf2Crr9OdAqpTzrPPPXgHDLWjpMgNsQNmvphuDD%2F%2BldMoH4pTHy3bUzbRkGgVk48cehUxzQ%3D&SiteSections_SiteSelector=0&SiteSections_CategorySelector=0&SiteSections_SubCategorySelector=0&start=2011-10-22&end=2011-10-22&ddlTop=40&txtDomain=&chkShowPV=on&chkShowIP=on&chkShowUV=on";
            return "";
        }

        public string GetData(string tableId, string idList)
        {
            string text = "";
            HtmlElement resultTable = webBrowser1.Document.GetElementById(tableId);
            if (resultTable != null)
            {
                HtmlElementCollection trColl = resultTable.GetElementsByTagName("tr");
                if (trColl != null)
                {
                    for (int i = 1; i < trColl.Count; i++)
                    {
                        try
                        {
                            if (trColl[i].GetElementsByTagName("td")[0].InnerText == CurrentData)
                            {
                                string[] idGroup = idList.Split(',');
                                for (int j = 0; j < idGroup.Length; j++)
                                {
                                    int id = int.Parse(idGroup[j]);
                                    text += " ";
                                    text += (trColl[i].GetElementsByTagName("td")[id].InnerText);
                                }
                            }
                        }
                        catch
                        {
                        }
                    }
                }
            }

            textBox1.Text += text;
            return text;
        }
        string nvUrl = "";
        string GetBaiduCount()
        {
            string count = "";
            HtmlElementCollection trColl = webBrowser1.Document.GetElementsByTagName("strong");
            if (trColl != null)
            {
                count = trColl[1].InnerText;
            }
            return count;
        }
        string GetGoogleCount()
        {
            string count = "";
            HtmlElement tb = webBrowser1.Document.GetElementById("resultStats");
            if (tb != null)
            {
                count = tb.InnerText;
            }
            return count;
        }
        string baiducount = "";
        void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            string tableid = "";
            string idList = "";

            string url = e.Url.ToString();
            if (url == "http://edit.autohome.com.cn/admin/SurveyAdmin/WebSiteFlow.aspx")
            {

                tableid = "tbResult";
                idList = "3,1";
                textBox1.Text += "流量统计新  uv    pv\r\n";
                string content = GetData(tableid, idList);
                if (!string.IsNullOrEmpty(content.Trim()))
                {
                    nvUrl = "http://edit.autohome.com.cn/admin/SurveyAdmin/UsedCarInput.aspx";
                    webBrowser1.Navigate(nvUrl);
                }
            }
            if (url == nvUrl)
            {
                tableid = "GridView1";
                idList = "10";
                textBox1.Text += "\r\n二手车入库  总数\r\n";
                string content = GetData(tableid, idList);
                if (!string.IsNullOrEmpty(content.Trim()))
                {
                    nvUrl = "  http://edit.autohome.com.cn/admin/SurveyAdmin/VisitSource.aspx";
                    string header = "Content-Type: application/x-www-form-urlencoded Content-Length: 650";
                    string postData = "__VIEWSTATE=JudEeGYjMIBO47PYD24K5I%2FvLgEOqTbMHT%2BkWfW0OTEW%2FHB0B4%2B64sUrHoNl7i%2BnOJc%2FCIXxihrbzz9FhNpclc8dRDL%2BZ4u%2FbGtaK%2FylPWLf4a4sZhX%2FOnoUxlIQYc%2B5tIuRBJ81tzpEufRPRz4thUDRtLxHp385jqBpINVJyWEYVqeirZNHFI3uoyW%2Fvt1Lzbgnc6O0bHhasgE3IbvKkYPCF18bAkj%2FFNHyaw%3D%3D&__EVENTVALIDATION=CdqGRY9mxFNpeSwLdH6yUTkB78t9iNuLzqlpi%2FG1uzlifzhxvtAFilxb75kuLU6q%2FIQ2g5ovl%2F%2FOFq00Gv2GZf2Crr9OdAqpTzrPPPXgHDLWjpMgNsQNmvphuDD%2F%2BldMoH4pTHy3bUzbRkGgVk48cehUxzQ%3D&SiteSections_SiteSelector=0&SiteSections_CategorySelector=0&SiteSections_SubCategorySelector=0&start=2011-10-22&end=2011-10-22&ddlTop=40&txtDomain=&chkShowPV=on&chkShowIP=on&chkShowUV=on";
                    postData = postData.Replace("%2F", "/");
                    postData = postData.Replace("%3D", "=");
                    postData = postData.Replace("%2B", "+");

                    byte[] postByte = System.Text.Encoding.ASCII.GetBytes(postData);
                    webBrowser1.Navigate(nvUrl, "mainFrame", postByte, header);
                }


            }


            if (url.IndexOf("google") > 0)
            {
                if (url.IndexOf(myUrlList[baiduIndex].Url.Substring(47, 20)) > 0)
                {
                    baiducount = GetGoogleCount();
                    if (!string.IsNullOrEmpty(baiducount.Trim()))
                    {
                        textBox1.Text += "\r\n" + myUrlList[baiduIndex].Title + "\r\n";

                        textBox1.Text += baiducount;
                        if (baiduIndex < myUrlList.Count)
                        {
                            baiduIndex++;



                        }
                    }
                }
            }
            if (url == myUrlList[baiduIndex].Url)
            {
                if (url.IndexOf("baidu") > 0)
                {
                    baiducount = GetBaiduCount();
                    if (!string.IsNullOrEmpty(baiducount.Trim()))
                    {
                        textBox1.Text += "\r\n" + myUrlList[baiduIndex].Title + "\r\n";

                        textBox1.Text += baiducount;
                        if (baiduIndex < myUrlList.Count)
                        {
                            baiduIndex++;
                            webBrowser1.Navigate(myUrlList[baiduIndex].Url);
                        }
                    }
                }
            }

            //if (webBrowser1.ReadyState != WebBrowserReadyState.Complete) return;

            //  if (e.Url.ToString() != webBrowser1.Url.ToString()) return;



            //if (!hasInput)
            //{
            //    HtmlElement btnSubmit = webBrowser1.Document.All[ConfigurationManager.AppSettings["loginField"].ToString()];
            //    HtmlElement tbUserid = webBrowser1.Document.All[ConfigurationManager.AppSettings["userNameField"].ToString()];
            //    HtmlElement tbPasswd = webBrowser1.Document.All[ConfigurationManager.AppSettings["passwordField"].ToString()];
            //    if (tbUserid == null || tbPasswd == null)
            //        return;
            //    tbUserid.SetAttribute("value", username);
            //    tbPasswd.SetAttribute("value", password);
            //    webBrowser1.Document.Forms[0].InvokeMember("submit");
            //    //  btnSubmit.InvokeMember("click");
            //    hasInput = true;
            //    return;
            //}


        }


        public void GoList()
        {

        }



        public void SignIn()
        {
            HtmlElement btnSignIN = webBrowser1.Document.All["view:_id1:btnSignIn"];

            if (btnSignIN != null)
            {
                object reobj = btnSignIN.InvokeMember("click");

            }
            this.Close();
        }
        public void SignOut()
        {
            HtmlElement btnSignIN = webBrowser1.Document.All["view:_id1:btnSignOut"];

            if (btnSignIN != null)
            {
                object reobj = btnSignIN.InvokeMember("click");

            }
            this.Close();
        }
        private void Form1_Shown(object sender, EventArgs e)
        {



        }

        private void btnFlowNew_Click(object sender, EventArgs e)
        {

            webBrowser1.Navigate("http://edit.autohome.com.cn/admin/SurveyAdmin/WebSiteFlow.aspx", false);

        }
        int baiduIndex = 0;
        private void btnBaidu_Click(object sender, EventArgs e)
        {
            if (baiduIndex < myUrlList.Count - 1)
            {
                webBrowser1.Navigate(myUrlList[baiduIndex].Url);
            }

        }
    }
    public class MyUrl
    {
        public string Title { get; set; }
        public string Url { get; set; }
    }
}
